<?php
include("../autoload.php");

$codAssessor = $_GET['cmbAssessor'];
$dataInicial = Util::dataToMysql($_GET['txtDataInicial']);
$dataFinal = Util::dataToMysql($_GET['txtDataFinal']);
$codCliente = $_GET['hdCodCliente'];
if(isset($_GET['cbMunicipios'])){
    $codMunicipio = $_GET['cbMunicipios'];
}else{
    $codMunicipio = false;
}
$strWhere = "";

if($dataInicial){
    $strWhere .= " AND SUBSTRING(data,1,10) >= '$dataInicial'";
    $strPeriodo = "A partir de ".Util::dataToBR($dataInicial);
}

if($dataFinal){
    $strWhere .= " AND SUBSTRING(data,1,10) <= '$dataFinal'";
    $strPeriodo = "Anterior a ".Util::dataToBR($dataFinal);
}

if($codCliente){
    $strWhere .= " AND imaps.cod_cliente = '$codCliente' ";
}

if($codMunicipio){
    $strWhere .= " AND clientes.codmunicipio = '$codMunicipio'";
}

$listaCheckin = imaps::lista_checkin(" 
    INNER JOIN 
        clientes ON clientes.codigo = imaps.cod_cliente
    WHERE 
        imaps.codusuario = '$codAssessor' 
        $strWhere  
    ORDER BY imaps.codigo DESC
");
    
?>
<table width="100%" style="border: 3px solid #000; background-color: white;">
    <?php
    if($listaCheckin){
    ?>
    <tr align="center">
        <td><strong>Data</strong></td>
        <td><strong>Cidade</strong></td>
        <td><strong>Estado</strong></td>
        <td><strong>UF</strong></td>
        <td><strong>Rua</strong></td>
        <td><strong>Bairro</strong></td>
        <td><strong>CEP</strong></td>
    </tr>
    <tr>
        <td colspan="7"><hr size="1" /></td>
    </tr>
    <?php
        foreach ($listaCheckin as $checkin){
            $aux = explode(" ",$checkin->getData());
            $data = $aux[0];
            $hora = $aux[1];
            
        ?>
            <tr>
                <td><?php echo Util::dataToBR($data)." ".$hora;?></td>
                <td><?php echo utf8_encode($checkin->getCidade());?></td>
                <td><?php echo utf8_encode($checkin->getEstado());?></td>
                <td><?php echo $checkin->getUf();?></td>
                <td><?php echo utf8_encode($checkin->getRua());?></td>
                <td><?php echo utf8_encode($checkin->getBairro());?></td>
                <td><?php echo $checkin->getCep();?></td>
            </tr>
    <?php
    }
    }else{
        ?>
            <tr> 
                <td align="center">N&atilde;o h&aacute; resultados</td>
            </tr>
        <?php
    }
?>
</table>